<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车牌号" prop="carNum">
        <el-input
          v-model="queryParams.carNum"
          placeholder="请输入车牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="到达日期" prop="arriveDate">
        <el-date-picker clearable
          v-model="queryParams.arriveDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择到达日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="绑定用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入绑定用户ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名字" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="identityNumber">
        <el-input
          v-model="queryParams.identityNumber"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="照片文件路径" prop="idcardUrlA">
        <el-input
          v-model="queryParams.idcardUrlA"
          placeholder="请输入照片文件路径"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="照片文件路径" prop="idcardUrlB">
        <el-input
          v-model="queryParams.idcardUrlB"
          placeholder="请输入照片文件路径"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="个人信息ID" prop="personalId">
        <el-input
          v-model="queryParams.personalId"
          placeholder="请输入个人信息ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车辆ID" prop="carId">
        <el-input
          v-model="queryParams.carId"
          placeholder="请输入车辆ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预约号码" prop="bookingNum">
        <el-input
          v-model="queryParams.bookingNum"
          placeholder="请输入预约号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="取号号码" prop="takeNum">
        <el-input
          v-model="queryParams.takeNum"
          placeholder="请输入取号号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['com.ruoyi.business:booking:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['com.ruoyi.business:booking:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['com.ruoyi.business:booking:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['com.ruoyi.business:booking:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bookingList" @selection-change="handleSelectionChange" >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column :resizable="false" type="index" align="center" label="序号" />
      <el-table-column label="车牌号" align="center" prop="carNum" />
            <el-table-column label="车辆类型 " align="center" prop="carType" >
              <template slot-scope="scope">
                <div v-if="scope.row.carType == 1">重车</div>
                <div v-else-if="scope.row.carType == 2">空车</div>

                <div v-else> </div>
              </template>
            </el-table-column>
            <el-table-column label="粮食类型" align="center" prop="foodType" >
              <template slot-scope="scope">
              <div v-if="scope.row.foodType == 1">稻谷</div>
              <div v-else-if="scope.row.foodType == 2">小麦</div>
              <div v-else-if="scope.row.foodType == 3">大米</div>
                <div v-else-if="scope.row.foodType == 4">油</div>
              <div v-else> </div>
            </template>
            </el-table-column>
      <el-table-column label="到达时段 " align="center" prop="arriveDateType" >
        <template slot-scope="scope">
          <div v-if="scope.row.arriveDateType == 1">上午</div>
          <div v-else-if="scope.row.arriveDateType == 2">下午</div>
          <div v-else> </div>
        </template>
      </el-table-column>
      <el-table-column label="到达日期" align="center" prop="arriveDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.arriveDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="用户名字" align="center" prop="userName" />
      <el-table-column label="手机号码" align="center" prop="phoneNumber" />
      <el-table-column label="照片文件路径" align="center" prop="idcardUrlA" width="100"/>
      <el-table-column label="照片文件路径" align="center" prop="idcardUrlB" width="100"/>
      <el-table-column label="个人信息ID" align="center" prop="personalId" width="90"/>
      <el-table-column label="车辆ID" align="center" prop="carId" />
      <el-table-column label="预约号码" align="center" prop="bookingNum" />
      <el-table-column label="状态 " align="center" prop="status" >
        <template slot-scope="scope">
          <div v-if="scope.row.status == 0">待叫号</div>
          <div v-else-if="scope.row.status == 1">待确认</div>
          <div v-else-if="scope.row.status == 2">纤样</div>
          <div v-else-if="scope.row.status == 3">过磅</div>
          <div v-else> </div>
        </template>
      </el-table-column>
      <el-table-column label="取号类型 " align="center" prop="bookingStatus" >
        <template slot-scope="scope">
          <div v-if="scope.row.bookingStatus == 1">预约取号</div>
          <div v-else-if="scope.row.bookingStatus == 2">现场取号</div>
          <div v-else> </div>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createdAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updatedAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="号码状态 " align="center" prop="numStatus" >
        <template slot-scope="scope">
          <div v-if="scope.row.numStatus == 0">待取号</div>
          <div v-else-if="scope.row.numStatus == 1">已取号</div>
          <div v-else-if="scope.row.numStatus == 2">已过期</div>
          <div v-else> </div>
        </template>
      </el-table-column>
      <el-table-column label="取号号码" align="center" prop="takeNum" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200" :fixed="fixedType">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="warning"
            @click="guobang(scope.row)"
            v-hasPermi="['com.ruoyi.business:booking:edit']"
          >通知取号</el-button>
          <!--<el-button
            size="mini"
            type="success"
            @click="guobang(scope.row)"
            v-hasPermi="['com.ruoyi.business:booking:edit']"
            v-if="scope.row.status == 1||scope.row.status == 0||scope.row.status == 2"
          >过磅</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['com.ruoyi.business:booking:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['com.ruoyi.business:booking:remove']"
          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车辆管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="个人信息ID" prop="personalId">
          <el-input v-model="form.personalId" placeholder="请输入个人信息ID" />
        </el-form-item>
        <el-form-item label="车辆ID" prop="carId">
          <el-input v-model="form.carId" placeholder="请输入车辆ID" />
        </el-form-item>
        <el-form-item label="车辆类型" prop="carType">
          <el-select v-model="form.carType" placeholder="选择车辆类型" clearable>
            <el-option label="重车" :value=1></el-option>
            <el-option label="空车" :value=2></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="粮食类型" prop="foodType">
          <el-select v-model="form.foodType" placeholder="选择粮食类型" clearable>
            <el-option label="稻谷" :value=1></el-option>
            <el-option label="小麦" :value=2></el-option>
            <el-option label="大米" :value=3></el-option>
            <el-option label="油" :value=4></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选择到达时段" prop="arriveDateType">
          <el-select v-model="form.arriveDateType" placeholder="选择到达时段" clearable>
            <el-option label="上午" :value=1></el-option>
            <el-option label="下午" :value=2></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="到达日期" prop="arriveDate">
          <el-date-picker clearable
            v-model="form.arriveDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择到达日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBooking, getBooking, delBooking, addBooking, updateBooking,notice } from "@/api/gwBooking/gwBooking";

export default {
  name: "Booking",
  data() {
    return {
      fixedType: 'right',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆管理表格数据
      bookingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        carNum: null,
        carType: null,
        foodType: null,
        arriveDateType: null,
        arriveDate: null,
        userId: null,
        userName: null,
        identityNumber: null,
        idcardUrlA: null,
        idcardUrlB: null,
        personalId: null,
        carId: null,
        bookingNum: null,
        status: null,
        bookingStatus: null,
        createdAt: null,
        updatedAt: null,
        numStatus: null,
        takeNum: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
        bookingStatus: [
          { required: true, message: "取号类型不能为空", trigger: "change" }
        ],
        numStatus: [
          { required: true, message: "号码状态不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询车辆管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.status = 0;
      //this.queryParams.bookingStatus = 1;
      listBooking(this.queryParams).then(response => {
        this.bookingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        bookingId: null,
        carNum: null,
        carType: null,
        foodType: null,
        arriveDateType: null,
        arriveDate: null,
        userId: null,
        userName: null,
        identityNumber: null,
        idcardUrlA: null,
        idcardUrlB: null,
        personalId: null,
        carId: null,
        delFlag: null,
        bookingNum: null,
        status: null,
        bookingStatus: null,
        remark: null,
        createdAt: null,
        updatedAt: null,
        numStatus: null,
        takeNum: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.bookingId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加预约管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const bookingId = row.bookingId || this.ids
      getBooking(bookingId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改预约管理";
      });
    },
    qianyang(row){
      const bookingIds = row.bookingId || this.ids;
      this.$modal.confirm('是否确认通知取号？').then(function() {
        return notice(bookingIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("通知取号成功");
      }).catch(() => {});;
    },
    guobang(row){
      const datas = row;
      datas.status = 1;
      this.$modal.confirm('是否确认通知取号？').then(function() {
        return updateBooking(datas);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("通知取号成功");
      }).catch(() => {});
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.bookingId != null) {
            updateBooking(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBooking(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const bookingIds = row.bookingId || this.ids;
      this.$modal.confirm('是否确认删除预约管理编号为"' + bookingIds + '"的数据项？').then(function() {
        return delBooking(bookingIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('com.ruoyi.business/booking/export', {
        ...this.queryParams
      }, `booking_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
